<?php
class ControllerCommonHome extends Controller {
	public function index() {
        //We will insert new roduct here here
        $time_file_name = 'system/cache/time.cachefile';
        if(!file_exists($time_file_name)) {
            fopen($time_file_name,'w') or die("Can't open file");
        }
        
        $time_ja = file_get_contents($time_file_name);
        if(time() - $time_ja >= 300 ) {
            file_put_contents($time_file_name,time());
            
            include "includes/function.php";
            include "libraries/xl_product.php";
            include "libraries/xl_product_description.php";
            include "libraries/xl_product_image.php";
            include "libraries/xl_product_to_category.php";
            include 'includes/simplehtmldom/simple_html_dom.php';
                
                
            $dt_xl_product = new xl_product;
            $dt_xl_product_image = new xl_product_image;
            $dt_xl_product_description = new xl_product_description;
            $dt_xl_product_to_category = new xl_product_to_category;    
            
            $website = "http://www.123mua.vn/thoi-trang-nu/page-1/?filter=3";
            
            $html = file_get_html($website);
            
            $art = $html->find('article',0);
            
            
            $product_link = $art->find('h2 a',0);
    		$product_name = $product_link->innertext();
            $product_image = $art->find('img',0);
            $product_price = $art->find('p.price',0);
            $product_price = $product_price->innertext;
            $product_price = convert_vi_to_en($product_price);
            $product_price = str_replace('-vnd','',$product_price);
            $product_price = str_replace('.','',$product_price);
            
            $img_src = file_get_contents($product_image->src);
            $arr = explode(".",$product_image->src);
            $extensions = $arr[count($arr)-1];
            $img_name = md5(time().rand(1,10000)).'.'.$extensions;
            file_put_contents($img_name,$img_src);
            
            $result = $dt_xl_product->them('0','import','100','7',$img_name,'0','1',
                $product_price,'0','','20','0','0','0','0','0','0','1','','','0','1','42','1','1','0');
            
            //Get the Inserted Product ID
            $product_id = $dt_xl_product->getInsert_id();
            
            //Bind the product to category
            $dt_xl_product_to_category->them($product_id,"122");
            
            
            
            $product_detail_page = "";
            $product_detail_page = file_get_html($product_link->href);
    		$product_description =  $product_detail_page->find('div.content-desc-product',0)->innertext;
            
            // Insert descriptions of this product into product_description table 
            $dt_xl_product_description->them($product_id,$product_name,convert_vi_to_en($product_name) , $product_description);
            
            // Get images of this product.
            // In this step, we can do some advanced work, such as move image into our site instead of getting these image's source.
    		$img_list = $product_detail_page->find('#box_thumbnail ul img');
    		foreach($img_list as $img) {
    			// Get image source
                $image = $img->src;
                
                // It is a thumbnail, it looks like xxxxxxxxx_130_130.jpg
                // But the large image has this style, xxxxxxxxx_574_0.jpg
                // So we use two following line to change thumbnail src to large image source and insert it to product_image 
                $image = str_replace('_130','_0',$image);
                $image = str_replace('_0','_574',$image);
                $img_src = file_get_contents($image);
                $arr = explode(".",$product_image->src);
                $extensions = $arr[count($arr)-1];
                $img_name = md5(time().rand(1,10000)).'.'.$extensions;
                file_put_contents("image/".$img_name,$img_src);
                $dt_xl_product_image->them($product_id,$img_name);
    		}
            file_put_contents($time_file_name,time());
        }    
        else {
        }
        
		$this->language->load('common/home');

		$this->document->title = sprintf($this->language->get('title'), $this->config->get('config_store'));
		$this->document->description = $this->config->get('config_meta_description');
		$this->document->home_flg    = "on";

		$this->document->breadcrumbs = array();

//      	$this->document->breadcrumbs[] = array(
//        	'href'      => $this->url->http('common/home'),
//        	'text'      => $this->language->get('text_home'),
//        	'separator' => FALSE
//      	);

		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_store'));
		$this->data['welcome'] = html_entity_decode($this->config->get('config_welcome_' . $this->language->getId()));

		$this->data['text_latest'] = $this->language->get('text_latest');

		$this->load->model('catalog/product');
		$this->load->model('catalog/review');
		$this->load->model('tool/seo_url');
		$this->load->helper('image');
		$this->data['products'] = array();
		$data = $this->model_catalog_product->getProductIndexPage();
		
		$this->data['product_data'] = $data;	
		if (!$this->config->get('config_customer_price')) {
			$this->data['display_price'] = TRUE;
		} elseif ($this->customer->isLogged()) {
			$this->data['display_price'] = TRUE;
		} else {
			$this->data['display_price'] = FALSE;
		}
		//--------------------------------------------------------------------//
		// Add template for home page
		//--------------------------------------------------------------------//
		$this->data['home_flg'] = "on";
		$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'common/home.phtml';
		$this->layout   = 'common/layout_index';
		$this->render();
	}
}
?>